﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using log4net;

namespace Koonsoft.Tool.CheckEveryday.Order.DAL
{
    public class ExceptionOrder
    {
        private readonly static ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private SqlConnection sqlconn;
        private SqlCommand sqlcmd;
        private SqlDataReader sqlrd;

        public ExceptionOrderInfo GetExceptionOrder()
        {
            ExceptionOrderInfo pollResult = new ExceptionOrderInfo
            {
                CommitingOrders = 0,
                WaitingConfirmButConfirmedOrders = 0,
                DispatchingButWithoutRequestOrders = 0,
                DispatchingButDispatchedOrders = 0,
                FinishedButStillOnDeliveringOrders = 0,
                CollectOnDeliveryButWaitingPayOrders = 0,
                PaidButWaitingPayOrders = 0,
                OvertimeImportTempOrders = 0
            };

            sqlconn = new SqlConnection();
            sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["ECSSCoreDB"].ConnectionString;

            sqlcmd = new SqlCommand();
            sqlcmd.Connection = sqlconn;
            sqlcmd.CommandText = "poll_StatisticExceptionorders";
            sqlcmd.CommandType = CommandType.StoredProcedure;

            try
            {
                sqlcmd.Connection.Open();

                sqlrd = sqlcmd.ExecuteReader();
                if (sqlrd.HasRows)
                {
                    sqlrd.Read();
                    pollResult.CommitingOrders = (int)sqlrd["CommitingOrders"];
                    pollResult.WaitingConfirmButConfirmedOrders = (int)sqlrd["WaittingConfirmOrders"];
                    pollResult.DispatchingButWithoutRequestOrders = (int)sqlrd["DispatchingButWithoutRequestOrders"];
                    pollResult.DispatchingButDispatchedOrders = (int)sqlrd["OnDispatchingOrders"];
                    pollResult.FinishedButStillOnDeliveringOrders = (int)sqlrd["FinishedButStillOnDeliveringOrders"];
                    pollResult.CollectOnDeliveryButWaitingPayOrders = (int)sqlrd["CollectOnDeliveryButWaitingPayOrders"];
                    pollResult.PaidButWaitingPayOrders = (int)sqlrd["PaidButWaitingPayOrders"];
                    pollResult.OvertimeImportTempOrders = (int)sqlrd["OvertimeImportTempOrders"];
                }
            }
            catch (Exception ex)
            {
                log.Error("巡检异常订单出错", ex);
            }
            finally
            {
                sqlcmd.Connection.Close();
            }

            return pollResult;
        }

    }
}
